package com.ruoyi.project.WxMoudle.api;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.interceptor.annotation.IgnoreAuth;
import com.ruoyi.project.WebMoudle.WxCitys.service.IWxCitysService;
import com.ruoyi.project.WebMoudle.WxShop.domain.WxShop;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WebMoudle.advShow.domain.AdvDistance;
import com.ruoyi.project.WebMoudle.advShow.domain.AdvShow;
import com.ruoyi.project.WebMoudle.advShow.domain.SearchShop;
import com.ruoyi.project.WebMoudle.advShow.service.IAdvShowService;
import com.ruoyi.project.WebMoudle.advertSeat.domain.AdvertSeat;
import com.ruoyi.project.WebMoudle.advertSeat.service.IAdvertSeatService;
import com.ruoyi.project.WebMoudle.useTerms.domain.UseTerms;
import com.ruoyi.project.WebMoudle.useTerms.service.IUseTermsService;
import com.ruoyi.project.WxMoudle.Bases.AppController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*广告逻辑*/
@RestController
@RequestMapping(value = "/wx/adv")
public class AdvController extends AppController {

    @Autowired
    private IAdvertSeatService advertSeatService;
    @Autowired
    private IAdvShowService advShowService;
    @Autowired
    private IUseTermsService termsService;
    @Autowired
    private IWxShopService wxShopService;
    @Autowired
    private IWxCitysService citysService;



    /*根据时间查询可以使用的广告位*/
    @RequestMapping(value = "/selectSeatList")
    @ResponseBody
    @IgnoreAuth
    public Object selectSeatList(String startTime,String endTime){
       List<AdvertSeat> list = advertSeatService.queryUsableSeat(startTime,endTime);
        return list;
    }

    /*申请使用广告位*/
    @RequestMapping(value = "/appplySeat")
    @ResponseBody
    @IgnoreAuth
    public Object appplySeat(MultipartFile[] imgs, AdvShow advShow) throws IOException {
        if (imgs != null) {
            for (MultipartFile file : imgs) {
                //上传图片
                String filePath2 = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String imgUrl = FileUploadUtils.upload(filePath2, file);
                if (StringUtils.isNotEmpty(advShow.getImgUrls())){
                   advShow.setImgUrls(advShow.getImgUrls()+","+imgUrl);
                }else {
                    advShow.setImgUrls(imgUrl);
                }
            }
        }else {
            //没有营业执照
            return toResponsFail("缺少图片");
        }
        advShowService.insertAdvShow(advShow);
        return writeResultRep();
    }

    /*根据店铺id查询申请列表*/
    @RequestMapping(value = "/querySeatListByShopId")
    @ResponseBody
    @IgnoreAuth
    public Object querySeatListByShopId(String shopId){
        AdvShow advShow = new AdvShow();
        advShow.setShopId(shopId);
        List<AdvShow> advShows = advShowService.selectAdvShowList(advShow);
        return advShows;
    }
    /*已合并到下面的接口----------   查询广告位商铺以及距离*/
    @RequestMapping(value = "/queryAdvList")
    @ResponseBody
    @IgnoreAuth
    public Object queryAdvList(String currentLat,String currentLng){
        List<AdvDistance> list = advShowService.queryAdvList(currentLat,currentLng);
        return list;
    }

    /*条件查询广告位商铺以及显示出距离*/
    @RequestMapping(value = "/searchShopList")
    @ResponseBody
    @IgnoreAuth
    public Object searchShopList(SearchShop ssp){
        if (StringUtils.isNotEmpty(ssp.getCityName()) && StringUtils.isNull(ssp.getCityId())){
            Long cityId =citysService.queryCityIdByName(ssp.getCityName());
            ssp.setCityId(cityId); 
        }
        Map<Object, Object> map = new HashMap<>();
        startPage();
        List<WxShop> list =wxShopService.searchShopList(ssp);
        List<UseTerms> UseTerms = termsService.selectUseTermsList(null);
        BigDecimal returnRatio = UseTerms.get(0).getReturnRatio();
        map.put("list",getDataTable(list)) ;
        map.put("returnRatio",returnRatio) ;
        return map;
    }

}
